//
//  DraggerView.m
//  ChimooTimer
//
//  Created by Ryan on 3/4/05.
//  Copyright 2005 Chimoosoft. All rights reserved.
//

#import "DraggerView.h"


@implementation DraggerView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
		[[self window] invalidateCursorRectsForView:self];
		
		lineColor = [[NSColor grayColor] retain];
		
		farEnough = NO;				
		
		// Create a path which is basically two vertical lines.  This will be
		// used for the user to click and drag.
		NSRect f = [self frame];
		NSPoint p;
		
		path = [[NSBezierPath bezierPath] retain];
				
		p.x = f.size.width / 2.0 - 1;
		p.y = 0;		
		[path moveToPoint:p];
		
		p.y = f.size.height;		
		[path lineToPoint:p];
		
		p.x = f.size.width = 2.0 + 1;
		[path moveToPoint:p];
		
		p.y = 0;
		[path lineToPoint:p];
		
		[path closePath];				
	
    }
    return self;
}


- (void)resetCursorRects {
	[self addCursorRect:[self bounds] cursor:[NSCursor openHandCursor]];
}

- (void)drawRect:(NSRect)rect {
    // Drawing code here.
	if (nil == path) return;
	if (nil == lineColor) return;
	
	[[lineColor colorWithAlphaComponent:0.5] set];
	
	[path stroke];	
}


- (void)mouseDown:(NSEvent *)theEvent {	
	
	startPoint = [theEvent locationInWindow];
	farEnough = NO;
	
	[lineColor release];
	lineColor = [[NSColor blueColor] retain];
	[self setNeedsDisplay:YES];
}	

- (void)mouseDragged:(NSEvent *)theEvent {
	
	NSPoint currentPoint = [theEvent locationInWindow];
	
	int threshold = 10;
	NSPoint delta;
	delta.x = startPoint.x - currentPoint.x;
	delta.y = startPoint.y - currentPoint.y;
			
	if (farEnough) {
		[self draggerDidMove:delta];
		startPoint.x = currentPoint.x;
		startPoint.y = currentPoint.y;
		return;
	}	
	
	if ((abs(delta.x) > threshold) || 
		(abs(delta.y) > threshold)) {
			farEnough = YES;
			// Then the user has dragged far enough away that we should detach the control window.
		[self draggerReachedBreakoffFromWindow:[self window]];
	}
	
}

- (void)mouseUp:(NSEvent *)theEvent {	
	[lineColor release];
	lineColor = [[NSColor grayColor] retain];
	[self setNeedsDisplay:YES];
	
	[self draggerMouseUp];
}


/*
- (void)mouseEntered:(NSEvent *)theEvent {
	NSLog(@"mouseEntered");
	cursor = [NSCursor closedHandCursor];
	[cursor push];
}

- (void)mouseExited:(NSEvent *)theEvent {
	NSLog(@"mouseExited");
	[cursor pop];
}
*/

- (void)dealloc {
	[lineColor release];
	[path release];
}


#pragma mark -
#pragma mark Delegate Methods

// These methods will send messages to the delegate of this class
// when certain interesting events occur.


- (id)delegate { return delegate; }

// Set the receiver's delegate to be aDelegate.
- (void)setDelegate:(id)aDelegate {
	// note, we don't want to retain this.  See 
	// http://cocoadevcentral.com/articles/000075.php for more info on this
	
	delegate = aDelegate;
}

// Called when the user drags past the breakoff threshold.
- (void)draggerReachedBreakoffFromWindow:(NSWindow*)window {
	if ([delegate respondsToSelector:@selector(draggerReachedBreakoffFromWindow:)]) {
		[delegate draggerReachedBreakoffFromWindow:window];
	}        
}

// End of dragging event.
- (void)draggerMouseUp {	
	if ([delegate respondsToSelector:@selector(draggerMouseUp)]) {
		[delegate draggerMouseUp];
	}        	
}


// Meant to be called after reaching the breakoff point.
- (void)draggerDidMove:(NSPoint)delta {
	if ([delegate respondsToSelector:@selector(draggerDidMove:)]) {
		[delegate draggerDidMove:delta];
	}        		
}

@end
